package com.zhuss.timer;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.zhuss.core.datasource1.dao.ext.BuildingDao;
import com.zhuss.core.datasource1.dao.ext.RoomDao;
import com.zhuss.core.datasource1.dao.ext.UnitDao;
import com.zhuss.core.datasource1.dao.ext.VillageDao;
import com.zhuss.core.datasource1.entity.Building;
import com.zhuss.core.datasource1.entity.Room;
import com.zhuss.core.datasource1.entity.Unit;
import com.zhuss.core.datasource1.entity.Village;
import com.zhuss.core.datasource2.dao.ext.lBuildingDao;
import com.zhuss.core.datasource2.dao.ext.lCommunityDao;
import com.zhuss.core.datasource2.dao.ext.lRoomDao;
import com.zhuss.core.datasource2.dao.ext.lUnitDao;


/**
 * start task
 * @author Administrator
 *
 */

@Component
@Configuration  
@EnableScheduling
public class StartTask {
	
	Logger  LOGGER = LoggerFactory.getLogger(StartTask.class);
	
	@Autowired
	private VillageDao villageDao;
	
	@Autowired
	private BuildingDao buildingDao;
	
	@Autowired
	private UnitDao unitDao;
	
	@Autowired
	private RoomDao roomDao;
	
	@Autowired
	private lCommunityDao lCommunityDao;
	
	@Autowired
	private lBuildingDao lBuildingDao;
	
	@Autowired
	private lUnitDao lUnitDao;
	
	@Autowired
	private lRoomDao lRoomDao;
	
	// 中林的自增ID对应的小区
	public static final Map<Long, Village> zlVillageIncrMap = new HashMap<>(); 
	public static final Map<Long, Building> zlBuildingIncrMap = new HashMap<>(); 
	public static final Map<Long, Unit> zlUnitIncrMap = new HashMap<>(); 
	
	
	// 初始化加载
	public void initVillage() {
		List<Village> vList = villageDao.listAll();
		vList.forEach(item -> zlVillageIncrMap.put(item.getZlIncr(), item));
	}
	// 初始化加载
	public void initBuilding() {
		List<Building> bList = buildingDao.listAll();
		bList.forEach(item -> zlBuildingIncrMap.put(item.getZlIncr(), item));
	}
	// 初始化加载
	public void initUnit() {
		List<Unit> vList = unitDao.listAll();
		vList.forEach(item -> zlUnitIncrMap.put(item.getZlIncr(), item));
	}
	
//	@Scheduled(cron = "1 1 1 * * ?")
	@Scheduled(cron = "0/20 * * * * ?")
	private void configureTasks() {
		
		LOGGER.error("开始执行：=====================" + new Date() + "==============");
		
		// 1、导入小区数据
		Long maxIncr = villageDao.selectMaxIncr();
		List<Village> villageList = lCommunityDao.queryList(maxIncr);
		for (Village v : villageList) {
			villageDao.create(v);
		}
		
		// 2、导入楼栋数据
		maxIncr = buildingDao.selectMaxIncr();
		List<Building> bList = lBuildingDao.queryList(maxIncr);
		
		for (Building b : bList) {
			try {
				// 设置 新表的 小区ID
				Long zlVillageId = b.getVillageId();
				if (zlVillageIncrMap.isEmpty() || zlVillageIncrMap.get(zlVillageId) == null) {
					initVillage();
				}
				Village village = zlVillageIncrMap.get(zlVillageId);
				b.setVillageId(village.getVillageId());
				b.setVillageName(village.getName());
				buildingDao.create(b);
			} catch (DuplicateKeyException e) {
				e.printStackTrace();
				continue;
			}
		}
		
		// 3、导入单元数据
		maxIncr = unitDao.selectMaxIncr();
		List<Unit> uList = lUnitDao.queryList(maxIncr);
		for (Unit u : uList) {
			try {
				// 设置 新表的 小区ID
				Long zlVillageId = u.getVillageId();
				if (zlVillageIncrMap.isEmpty() || zlVillageIncrMap.get(zlVillageId) == null) {
					initVillage();
				}
				Village village = zlVillageIncrMap.get(zlVillageId);
				// 单元小区为空， 跳过
				if (village == null) {
					continue;
				}
				u.setVillageId(village.getVillageId());
				u.setVillageName(village.getName());
				// 设置 新表的 楼栋ID
				Long zlBuildingId = u.getBuildingId();
				if (zlBuildingIncrMap.isEmpty() || zlBuildingIncrMap.get(zlBuildingId) == null) {
					initBuilding();
				}
				Building building = zlBuildingIncrMap.get(zlBuildingId);
				// 单元楼栋为空， 跳过
				if (building == null) {
					continue;
				}
				u.setBuildingId(building.getBuildingId());
				u.setBuildingName(building.getName());
				unitDao.create(u);
			} catch (DuplicateKeyException e) {
				e.printStackTrace();
				continue;
			}
		}
		
		
		// 4、导入门牌号数据
		maxIncr = roomDao.selectMaxIncr();
		List<Room> rList = lRoomDao.queryList(maxIncr);
		for (Room r : rList) {
			try {
				// 设置 新表的 小区ID
				Long zlVillageId = r.getVillageId();
				if (zlVillageIncrMap.isEmpty() || zlVillageIncrMap.get(zlVillageId) == null) {
					initVillage();
				}
				Village village = zlVillageIncrMap.get(zlVillageId);
				// 门牌号小区为空， 跳过
				if (village == null) {
					continue;
				}
				r.setVillageId(village.getVillageId());
				r.setVillageName(village.getName());
				// 设置 新表的 楼栋ID
				Long zlBuildingId = r.getBuildingId();
				if (zlBuildingIncrMap.isEmpty() || zlBuildingIncrMap.get(zlBuildingId) == null) {
					initBuilding();
				}
				Building building = zlBuildingIncrMap.get(zlBuildingId);
				// 门牌号楼栋为空， 跳过
				if (building == null) {
					continue;
				}
				r.setBuildingId(building.getBuildingId());
				r.setBuildingName(building.getName());
				// 设置 新表的 单元ID
				Long zlUnitId = r.getUnitId();
				if (zlUnitIncrMap.isEmpty() || zlUnitIncrMap.get(zlUnitId) == null) {
					initUnit();
				}
				Unit unit = zlUnitIncrMap.get(zlUnitId);
				// 门牌号单元为空， 跳过
				if (unit == null) {
					continue;
				}
				r.setUnitId(unit.getUnitId());
				r.setUnitName(unit.getName());
				roomDao.create(r);
			} catch (DuplicateKeyException e) {
				e.printStackTrace();
				continue;
			}
		}
		
		System.err.println("BuildingTask-------执行静态定时任务时间: " + LocalDateTime.now());
	}
	

}
